package c;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

class Person {
    private int id;
    private String name;
    
    public Person (int id, String name) {
    	this.id = id;
    	this.name = name;
    }
    
    public String toString() {
    	return "{ID is : " + id + " name is : " + name + "}";
    }

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	} 
}

public class CustomCollection {

	public static void main(String[] args) {
		
		Person p1 = new Person(0, "Bob");
		Person p2 = new Person(1, "Alice");
		Person p3 = new Person(2, "Carl");
		Person p4 = new Person(0, "Bob");
		
        Map<Person, Integer> map = new LinkedHashMap<Person, Integer>();
        
        map.put(p1, 1);
        map.put(p2, 3);
        map.put(p3, 4);
        map.put(p4, 1);
        
        for(Map.Entry<Person, Integer> entry : map.entrySet()) {
        	System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        
        Set<Person> set = new LinkedHashSet<Person>();
        set.add(p1);
        set.add(p2);
        set.add(p3);
        set.add(p4);
        
        System.out.println(set);
	}

}
